// import Vue from "vue"
// import {allExpandedKeys, allPermission} from "_router/permissionsKeys"
import routers from "_router/ALL_ROUTES"
import config from "../../config"
// import request from "_request"

import {getMenuByRouter} from "_libs/routerUtils.js"

export default {
    state: {
        // 所有权限的枚举
        allPermissionKeys: [],
        expandedKeys: [],
        // 当前允许使用的权限key
        baseInfo: {}, // 系统的基本可公开信息
        userData: {},
    },
    getters: {
        menuList: (state, getters, rootState) => getMenuByRouter(routers, [])
    },
    mutations: {
        setRole(state, role) {
            state.allRole = role
        },
        setUserMsg(state, data) {
            let {userData, baseInfo} = data
            state.userData = userData
            state.baseInfo = baseInfo
        },
        clearUserData(state, data) {
            state.userData = {}
            state.baseInfo = {}
        }
    },
    actions: {
        handleLogOut({commit, dispatch}) {
            const key = "__USER_KEY__"
            const localData = localStorage.getItem(key)
            const {token, session_key} = localData && JSON.parse(localData) || {}
            localStorage.removeItem(key)
            if (token && session_key) {
                window.location.href = config.apiDomainList.loginbaseUrl + `/logout?token${token}&session_key${session_key}`
            }
        },
        handleGetUserMsg({commit}) {
            // 模拟获取用户信息
            return new Promise((resolve, reject) => {
                // let initData = window && window.__INITIAL_STATE__ || {}
                // if (Object.keys(initData).length) {
                //     commit("setUserMsg", initData)
                //     resolve(initData)
                // } else {
                //     resolve(null)
                // }
                const initData = {
                    userData: {
                        user_name: "俊杰",
                        token: "21321312"
                    }, baseInfo: {}
                }
                commit("setUserMsg", initData)
                resolve(initData)
            })
        }
    }
}
